package com.rui.study.micro.template.manage.service.impl;

import com.rui.study.commons.model.BizException;
import com.rui.study.micro.template.manage.mapper.AnnMapper;
import com.rui.study.micro.template.manage.model.AnnPO;
import com.rui.study.micro.template.manage.service.AnnService;
import com.rui.study.micro.template.manage.model.AddAnnDTO;
import com.rui.study.micro.template.manage.util.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AnnServiceImpl implements AnnService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AnnServiceImpl.class);

    @Autowired
    private AnnMapper templateMapper;


    @Override
    public void addAnn(AddAnnDTO addAnnDTO) {
        AnnPO annPO = new AnnPO();
        BeanUtils.copyProperties(addAnnDTO, annPO);
        int result = templateMapper.insertAnn(annPO);
        if (result < 1)
            throw new BizException(StatusCode.INSERT_EXCEPTION);
        LOGGER.info("Succeed to add a new announcement [{}]", annPO.getAnnId());
    }

    @Override
    public AnnPO queryAnn(String annId) {
        AnnPO annPO = templateMapper.selectAnn(annId);
        if (annPO == null)
            throw new BizException(StatusCode.ANNOUNCEMENT_NOT_EXISTS);
        return annPO;
    }
}
